import {createApp} from 'vue'
// import './style.css'
import App from './App.vue'
import router from './router'
import {start, close} from './utils/nprogress';
import {userName,tokenName} from "./config";
import TinyThemeTool from '@opentiny/vue-theme/theme-tool.js'
import {tinySmbTheme} from '@opentiny/vue-theme/theme' // 导入主题
import "@icon-park/vue-next/styles/index.css";
import {createPinia} from 'pinia'

const pinia = createPinia()
import UndrawUi from 'undraw-ui'
import 'undraw-ui/dist/style.css'


const app = createApp(App)

const theme = new TinyThemeTool(tinySmbTheme, 'tinyStyleSheetId') // 初始化主题
// app.config.globalProperties.$theme = theme;


// 全局路由守卫
router.beforeEach((to, from, next) => {
    start()
    if (to.path === '/login') {
        next()
    } else if (!JSON.parse(localStorage.getItem(userName))) {
        if (to.meta.isAuth){
            next({path: '/login'})
        }else {
            next()
        }
    } else {
        // 获取当前登录用户
        const currentUser = JSON.parse(localStorage.getItem(userName));
        // 检查密码是否过期
        if (new Date().getTime() > currentUser.expire) {
            // 移除
            localStorage.removeItem(userName);
            localStorage.removeItem(tokenName);
        }
        next()
    }
})
router.afterEach(() => {
    close()
})


app.use(router)
app.use(pinia)
app.use(UndrawUi)


app.mount('#app')
